// pages/board/board.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_type: 'rank',
    gid: '',
    is_admin: false,
    items: {
      rank: {
        page: 1,
        self: '',
        list: [],
        no_more: false
      },
      points: {
        page: 1,
        self: '',
        list: [],
        no_more: false
      },
      score: {
        page: 1,
        self: '',
        list: [],
        no_more: false
      }
    },
    show_rank_input: false,
    show_all_input: false,
    rank_input_list: [],
    rank_input_id: '',
    rank_key: 0,
    region: ['湖南省', '株洲市', '芦淞区'],
    show_search: false,
    search_list: [],
    top_color: ['gold', 'silver', 'brown'],
    city: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
    })
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  onSearchConfirm: function (e) {
    const that = this
    const word = e.detail.value
    if (!word.trim()) {
      return
    }
    app.request({
      url: app.root + 'board/search',
      method: 'post',
      data: {
        gid: that.data.gid,
        word: word,
        list_type: that.data.list_type
      },
      success: function (res) {
        console.log(res)
        if(!res.data.err){
          that.setData({
            show_search: true,
            search_list: res.data.list,

          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    wx.getShareInfo({
      shareTicket: app.app_options.shareTicket,
      success: function (res) {
        wx.showLoading({
          title: '读取中',
        })
        app.request({
          url: app.root + 'board',
          method: 'post',
          data: {
            encryptedData: res.encryptedData,
            iv: res.iv,
            list_type: 'rank'
          },
          success: function (res) {
            console.log(res)
            that.setData({
              gid: res.data.gid,
              'items.rank.self': res.data.self,
              rank_key: res.data.self.rank_key,
              'items.rank.list': res.data.list,
              'items.rank.no_more': res.data.no_more,
              rank_input_list: res.data.rank_input_list,
              show_all_input: res.data.show_all_input,
              is_admin: res.data.is_admin,
              city: res.data.city
            })
            wx.hideLoading()
          }
        })
      },
      fail: function (res) {
        console.log(res)
        wx.showLoading({
          title: '读取中',
        })
        app.request({
          url: app.root + 'board',
          method: 'post',
          data: {
            list_type: 'rank'
          },
          success: function (res) {
            that.setData({
              'items.rank.self': res.data.self,
              rank_key: res.data.self.rank_key,
              'items.rank.no_more': res.data.no_more,
              rank_input_list: res.data.rank_input_list,
              show_all_input: res.data.show_all_input,
              city: res.data.city
            })
            wx.hideLoading()

          }
        })
      }
    })
  },

  onNavBtn: function (e) {
    const type = e.currentTarget.dataset.type
    const that = this
    that.setData({
      list_type: type,
      show_search: false
    })

    if (that.data.items[type].no_more) {
      return
    }

    app.request({
      url: app.root + 'board',
      method: 'post',
      data: {
        gid: that.data.gid,
        page: that.data.items[type].page,
        list_type: type
      },
      success: function (res) {
        switch (type) {
          case 'points':
            that.setData({
              'items.points.self': res.data.self,
              'items.points.list': res.data.list,
              'items.points.no_more': res.data.no_more,
            });
            break;
          case 'score':
            that.setData({
              'items.score.self': res.data.self,
              'items.score.list': res.data.list,
              'items.score.no_more': res.data.no_more,
            });
            break;
        }
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this
    const type = that.data.list_type
    if (type == 'search' || that.data.items[type].no_more) return

    app.request({
      url: app.root + 'board',
      method: 'post',
      data: {
        gid: that.data.gid,
        page: ++that.data.items[type].page,
        list_type: type
      },
      success: function (res) {
        switch (type) {
          case 'rank':
            that.setData({
              'items.rank.page': that.data.items[type].page,
              'items.rank.list': that.data.items[type].list.concat(res.data.list),
              'items.rank.no_more': res.data.no_more,
            });
            break;
          case 'points':
            that.setData({
              'items.points.page': that.data.items[type].page,
              'items.points.list': that.data.items[type].list.concat(res.data.list),
              'items.points.no_more': res.data.no_more,
            });
            break;
          case 'score':
            that.setData({
              'items.score.page': that.data.items[type].page,
              'items.score.list': that.data.items[type].list.concat(res.data.list),
              'items.score.no_more': res.data.no_more,
            });
            break;
        }
      }
    })
  },

  onAccuseBtn: function (e) {
    const uid = e.currentTarget.dataset.uid
    const nickname = e.currentTarget.dataset.nickname

    wx.showModal({
      title: '提示',
      content: '确定举报' + nickname + '吗？',
      success: function (res) {
        if (res.confirm) {
          app.request({
            url: app.root + 'board/accuse',
            method: 'post',
            data: {
              uid: uid,
            },
            success: function (res) {
              wx.showModal({
                title: '提示',
                content: res.data.msg,
              })
            }

          })
        }
      }
    })

  },

  onRankInput: function (e) {
    const uid = e.currentTarget.dataset.uid
    const nickname = e.currentTarget.dataset.nickname
    const rank = e.currentTarget.dataset.rank
    this.setData({
      rank_input_id: uid,
      show_rank_input: true,
      rank_key: rank
    })

  },

  onAllConfirm: function(e){
    const rank = this.data.rank_key
    const region = this.data.region
    const that = this

    if(rank == 0) return;
    

    app.request({
      url: app.root + 'board/update',
      method: 'post',
      data: {
        rank: rank,
        region: region
      },
      success: function (res) {
        wx.showModal({
          title: '提示',
          content: '修改成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
             wx.redirectTo({
               url: '/pages/board/board',
             })
            }
          }
        })
      }

    })

  },

  onRankConfirm: function (e) {
    const uid = this.data.rank_input_id
    const rank = this.data.rank_key
    const that = this
    app.request({
      url: app.root + 'board/update',
      method: 'post',
      data: {
        uid: uid,
        rank: rank
      },
      success: function (res) {
        if(that.data.show_search){
          let search_list = that.data.search_list
          for (let i in search_list) {
            if (search_list[i]['id'] == uid) {
              search_list[i]['rank'] = that.data.rank_input_list[rank]
              search_list[i]['rank_key'] = rank
              search_list[i]['accuse_num'] = 0
            }
          }
          that.setData({
            search_list: search_list,
            show_rank_input: false
          })
        }else{
          let rank_list = that.data.items.rank.list
          for (let i in rank_list) {
            if (rank_list[i]['id'] == uid) {
              rank_list[i]['rank'] = that.data.rank_input_list[rank]
              rank_list[i]['rank_key'] = rank
              rank_list[i]['accuse_num'] = 0
            }
          }

          that.setData({
            'items.rank.list': rank_list,
            show_rank_input: false
          })
        }
        
        wx.showModal({
          title: '提示',
          content: '修改成功',
        })
      }

    })
  },

  onRankCancel: function (e) {
    this.setData({
      show_rank_input: false,
      show_all_input: false
    })
  },

  onRankSelect: function (e) {
    this.setData({
      rank_key: e.detail.value
    })
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})